package com.jiuyi.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiuyi.common.R;
import com.jiuyi.dto.AdminSetmealDto;
import com.jiuyi.entity.Dish;
import com.jiuyi.entity.Employee;
import com.jiuyi.entity.Setmeal;
import com.jiuyi.entity.SetmealDish;
import com.jiuyi.exception.SystemException;
import com.jiuyi.mapper.CategoryMapper;
import com.jiuyi.mapper.EmployeeMapper;
import com.jiuyi.mapper.SetmealDishMapper;
import com.jiuyi.mapper.SetmealMapper;
import com.jiuyi.service.EmployeeService;
import com.jiuyi.service.SetmealService;
import com.jiuyi.utils.BeanCopyUtils;
import com.jiuyi.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

import static com.jiuyi.emum.HttpMsgMenu.SETMEAL_NO_DELETE;

/**
 * DATA: 2022/10/19
 * Author: 晨夕
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private CategoryMapper categoryMapper;

    //添加套餐
    @Transactional
    @Override
    public R addSetmeal(AdminSetmealDto adminSetmealDto) {
        //1. 向套餐表中添加
        Setmeal setmeal = BeanCopyUtils.copyBean(adminSetmealDto, Setmeal.class);
        setmealMapper.insert(setmeal);
        //2. 向套餐菜品表中添加
        List<AdminSetmealDishVo> setmealDisheVos = adminSetmealDto.getSetmealDishes();
        List<SetmealDish> setmealDishlist = BeanCopyUtils.copyBeanList(setmealDisheVos, SetmealDish.class);
        for (SetmealDish setmealDish : setmealDishlist){
            setmealDish.setSetmealId(setmeal.getId().toString());
            setmealDishMapper.insert(setmealDish);
        }
        return R.success("添加成功");
    }
    //分页查询套餐
    @Override
    public R<AdminSelectSetmealVo> selectSetmealList(int page, int pageSize, String name) {
        //1. 根据姓名查询，并根据修改时间降序排序
        List<Setmeal> setmealList = setmealMapper.selectSetmealList((page - 1) * pageSize,pageSize,name);
        int count = setmealMapper.selectSetmealCount(name);
        List<AdminSelectSetmealVo> adminSelectSetmealVos = BeanCopyUtils.copyBeanList(setmealList, AdminSelectSetmealVo.class);
        //对每个分类赋值
        for(AdminSelectSetmealVo adminSelectSetmealVo : adminSelectSetmealVos){
            String categoryName = categoryMapper.selectByCategoryId(adminSelectSetmealVo.getCategoryId());
            adminSelectSetmealVo.setCategoryName(categoryName);
        }
        //2. 分页
        PageVo<AdminSelectSetmealVo> pageVo = new PageVo<>();
        pageVo.setRecords(adminSelectSetmealVos);
        pageVo.setTotal(count);
        return R.ok(pageVo);
    }
    //删除套餐
    @Override
    public R deleteSetmeal(Long[] ids) {
        //2. 删除已经停售了的套餐，起售的套餐无法删除
        for(Long id : ids){
            //通过id获取status
            int status = setmealMapper.selectStatusById(id);
            //status为1就不能删除，抛出异常，并启动事务
            if(status == 1){
                throw new SystemException(SETMEAL_NO_DELETE);
            }
            //1. 删除套餐菜单表中的数据
            List<Long> setMealDishIds = setmealDishMapper.selectBySetMealId(id);
            for(Long setMealDishId : setMealDishIds){
                setmealDishMapper.deleteById(setMealDishId);
            }
            setmealMapper.deleteById(id);
        }
        return R.success("删除成功");
    }
    //停售套餐
    @Override
    public R stopSetmeal(Long[] ids) {
        //将status修改为0
        setmealMapper.updateStatusById(ids);
        return R.success("停售成功");
    }
    //起售套餐
    @Override
    public R startSetmeal(Long[] ids) {
        //将status修改为1
        setmealMapper.updateStatusByIds(ids);
        return R.success("起售成功");
    }
    //根据id查询套餐
    @Override
    public R<AdminUpdateSetmealVo> selectById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        AdminUpdateSetmealVo adminUpdateSetmealVo = BeanCopyUtils.copyBean(setmeal, AdminUpdateSetmealVo.class);
        //查询菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.selectDishById(setmeal.getId());
        List<AdminSetmealDishVo> adminSetmealDishVos = BeanCopyUtils.copyBeanList(setmealDishes, AdminSetmealDishVo.class);
        adminUpdateSetmealVo.setSetmealDishes(adminSetmealDishVos);
        return R.ok(adminUpdateSetmealVo);
    }
    //修改套餐
    @Override
    public R updateSetmeal(AdminSetmealDto adminSetmealDto) {
        //1. 修改套餐
        Setmeal setmeal = BeanCopyUtils.copyBean(adminSetmealDto, Setmeal.class);
        //2. 修改套餐菜品
        setmealMapper.updateById(setmeal);
        //2.1 删除套餐菜品
        setmealDishMapper.deleteBySetmealId(setmeal.getId());
        //2.1 添加套餐菜品
        List<AdminSetmealDishVo> setmealDishes = adminSetmealDto.getSetmealDishes();
        for(AdminSetmealDishVo adminSetmealDishDto : setmealDishes){
            SetmealDish setmealDish = BeanCopyUtils.copyBean(adminSetmealDishDto, SetmealDish.class);
            setmealDish.setSetmealId(setmeal.getId().toString());
            setmealDishMapper.insert(setmealDish);
        }
        return R.success("修改成功");
    }
    //根据category_id查询套餐集合
    @Override
    public R<SetMealListVo> selectListById(Long categoryId) {
        List<Setmeal> setmeals = setmealMapper.selectListById(categoryId);
        List<SetMealListVo> setMealListVos = BeanCopyUtils.copyBeanList(setmeals, SetMealListVo.class);
        return R.ok(setMealListVos);
    }
    //根据id查询
    @Override
    public R<Setmeal> selectDishById(Long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        return R.ok(setmeal);
    }

}
